package com.ruoyi.project.g6.tools;

import com.ruoyi.project.g6.entity.Vehicle;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

public class SetDefaultValueBean {

    private static final String defaultStr = "";
    private static final Date defaultDate = new Date();
    private static final BigDecimal defaultDecimal = new BigDecimal(0);
    private static final Timestamp defaultTimestamp = new Timestamp(System.currentTimeMillis());

    public static void main(String[] args) {
        SetDefaultValueBean setDefaultValueBean = new SetDefaultValueBean();
        Vehicle vehicle = new Vehicle();
        vehicle.setVehicleLicense("GB17691-2005");
        System.out.println(vehicle);
    }

    //赋默认值
    public static void setDefaultValue(Object object) {
        try {
            Class clazz = object.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                String fieldName = field.getName();
                Class fieldClass = field.getType();
                field.setAccessible(true); //设置访问权限
                if (isFieldValueNull(fieldName, object)) {
                    if (fieldClass == Integer.class) {
                        field.set(object, defaultDecimal.intValue());
                    } else if (fieldClass == Long.class) {
                        field.set(object, defaultDecimal.longValue());
                    } else if (fieldClass == Float.class) {
                        field.set(object, defaultDecimal.doubleValue());
                    } else if (fieldClass == BigDecimal.class) {
                        field.set(object, defaultDecimal);
                    } else if (fieldClass == Date.class) {
                        field.set(object, defaultDate);
                    } else if (fieldClass == String.class) {
                        field.set(object, defaultStr); // 设置值
                    } else if (fieldClass == Timestamp.class) {
                        field.set(object, defaultTimestamp);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    //判断字段是否为空
    private static boolean isFieldValueNull(String fieldName, Object object) throws ClassNotFoundException {
        boolean isNUll = false;
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            if (value == null) {
                isNUll = true;
            }
            return isNUll;
        } catch (Exception e) {
            return isNUll;
        }
    }

    //判断主键是否为空值
    private boolean isStringFieldValueNull(String fieldName, Object object, Class fieldClass) throws ClassNotFoundException {
        boolean isNUll = false;
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            if (value == null) {
                isNUll = true;
            } else {
                if (fieldClass == String.class && StringUtils.isBlank((String) value)) {
                    isNUll = true;
                }
            }
            return isNUll;
        } catch (Exception e) {
            return isNUll;
        }
    }
}
